using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Pampa.WebControls;
using Pampa.CRUD.UI.Exceptions;
using Pampa.CRUD.Configuration;
using Pampa.CRUD.Configuration.Validations;

namespace Pampa.CRUD.UI.CustomControls
{
    [ToolboxData("<{0}:EditableDetail runat=server></{0}:EditableDetail>")]
    public class EditableDetail : EditableGroup
    {
        private string parentID;
        private string rowId;
        
        private bool removeButton = true;
        private bool cloneButton = true;
        private bool hidden = false;

        List<ItemLink> links;

        public EditableDetail()
        {
            links = new List<ItemLink>();
        }
        
        #region Properties

        public int ItemCount
        {
            get { return itemList.Count; }
        }

        public List<string> Items
        {
            get { return itemOrderList; }
        }

        public string ParentID
        {
            get { return parentID; }
            set { parentID = value; }
        }

        public string RowId
        {
            get { return rowId; }
            set { rowId = value; }
        }
        
        public bool RemoveButton
        {
            get { return removeButton; }
            set { removeButton = value; }
        }

        public bool CloneButton
        {
            get { return cloneButton; }
            set { cloneButton = value; }
        }

        public bool Hidden
        {
            get { return hidden; }
            set { hidden = value; }
        }

        public List<ItemLink> Links
        {
            get { return links; }
            set { links = value; }
        }

        #endregion

        #region Public Functions

        public Label GetItemCaption(int columnIndex)
        {
            string itemId;

            try
            {
                itemId = this.itemOrderList[columnIndex];
            }
            catch (ArgumentOutOfRangeException)
            {
                throw new EditException("No se pudo hallar el item para la columna especificada. Columna Nro.: " + columnIndex + ".");
            }

            return GetCaption(itemId);
        }

        public EditableDetail Clone()
        {
            return Clone("");
        }

        public EditableDetail Clone(string newDetailName)
        {
            EditableDetail newDetail = new EditableDetail();
            newDetail.ID = newDetailName;
            newDetail.RemoveButton = this.RemoveButton;
            newDetail.CloneButton = this.CloneButton;
            newDetail.ParentID = this.ParentID;
            newDetail.PathImages = this.PathImages;

            for (int i = 0; i < itemOrderList.Count; i++)
            {
                CloneItem(itemOrderList[i], newDetail);
            }

            return newDetail;
        }

        public void AddItemLink(ItemLink link)
        {
            this.Links.Add(link);
        }

        #endregion

        #region Render

        protected override void Render(HtmlTextWriter writer)
        {
            string itemId;
            Control controlItem;

            if (this.Hidden)
            {
                writer.AddStyleAttribute("display", "none");
            }

            writer.AddAttribute("id", "row_" + this.ID);
            writer.RenderBeginTag(HtmlTextWriterTag.Tr);
            
            for (int i = 0; i < itemOrderList.Count; i++)
            {
                itemId = itemOrderList[i];
                controlItem = GetControl(itemId);

                writer.AddAttribute(HtmlTextWriterAttribute.Class, "controlCell");
                writer.RenderBeginTag(HtmlTextWriterTag.Td);
                controlItem.RenderControl(writer);
                writer.RenderEndTag();
            }

            RenderActions(writer);

            writer.RenderEndTag(); // row
        }

        protected void RenderActions(HtmlTextWriter writer)
        {
            // Agrega la columna con las acciones de la fila.
            if ((this.RemoveButton) || (this.CloneButton))
            {
                writer.AddAttribute(HtmlTextWriterAttribute.Nowrap, null);
                writer.AddAttribute(HtmlTextWriterAttribute.Class, "controlCell");
                writer.AddStyleAttribute(HtmlTextWriterStyle.TextAlign, "center");
                writer.RenderBeginTag(HtmlTextWriterTag.Td);

                // Boton eliminar
                if (this.RemoveButton)
                {
                    System.Web.UI.WebControls.Image btn = new Image();
                    btn.ID = "trash";
                    btn.ImageUrl = GetResourceUrl("Pampa.CRUD/trash.gif");
                    btn.ToolTip = Resources.EdiatbleDetails.Delete;
                    btn.Style.Add(HtmlTextWriterStyle.Cursor, "pointer");
                    btn.Attributes.Add("onclick", "EditableDetails_RemoveItem(\"" + this.ParentID + "\", \"" + this.ID + "\")");
                    btn.RenderControl(writer);
                }

                // Boton duplicar
                if (this.CloneButton)
                {
                    System.Web.UI.WebControls.Image btn2 = new Image();
                    btn2.ID = "duplicate";
                    btn2.ImageUrl = GetResourceUrl("Pampa.CRUD/duplicate.gif");
                    btn2.ToolTip = Resources.EdiatbleDetails.Duplicate;
                    btn2.Style.Add(HtmlTextWriterStyle.Cursor, "pointer");
                    btn2.Attributes.Add("onclick", "EditableDetails_DuplicateItem(\"" + this.ParentID + "\", \"" + this.ID + "\")");
                    btn2.RenderControl(writer);
                }

                writer.RenderEndTag();
            }
        }
        
        #endregion

        #region Private Functions

        private void CloneItem(string itemID, EditableDetail newDetail)
        {
            VisibleItem item = (VisibleItem)this.itemList[itemID];
            newDetail.AddItem(item.Clone());
        }

        #endregion
    }
}
